<!DOCTYPE html>
<html>
<head>
    <title>打印 </title>
    {% include 'system/common/header.html' %}
</head>
<body>
<form class="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div id="printView" >{{ printType }}</div>
                <div class="layui-form-item">
                    <label class="layui-form-label">报表</label>
                    <div class="layui-input-block">
                            <select id="select-filter">
                                {% for report in reports %}
                                  <option value="{{ report.id }}">{{ report.data_label }}</option>
                                 {% endfor %}
                            </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">打印机</label>
                    <div class="layui-input-block">
                            <select id="select-printer">

                            </select>
                    </div>
                </div>


            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                确认
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
{% include 'system/common/footer.html' %}
<script>
  layui.use(['form', 'jquery'], function () {
    let form = layui.form;
    let $ = layui.jquery;
    // 获取本地打印机
    $.ajax({
        url: 'http://localhost:5003/print',
        data: '',
        dataType: 'json',
        contentType: 'application/json',
        type: 'get',
        success: function (result) {

            var select=document.getElementById('select-printer')
            var options=result.printers

            options.forEach(function (option) {
                var opt=document.createElement('option');
                opt.value=option.value;
                opt.text=option.text;
                select.appendChild(opt);

            })
            form.render('select')

        },

    })
    form.on('submit(user-save)', function (data) {

        var value = $('#select-filter').val();

        var printer = $("#select-printer option:selected").text();
        var printType= $("#printView").text();
        data.field.value=value;
        data.field.printer=printer;
        data.field.printType=printType;
          $.ajax({
            url: '/system/user/main/print',
            data: JSON.stringify(data.field),
            dataType: 'json',
            contentType: 'application/json',
            type: 'post',
            success: function (result) {
                if (result.success) {
                    layer.msg(result.msg, {icon: 1, time: 1000}, function () {


                        // 把数据发送到本地
                        $.ajax({
                            url: 'http://localhost:5003/print',
                            data: JSON.stringify(result),
                            dataType: 'json',
                            contentType: 'application/json',
                            type: 'post',
                            success: function (result) {

                                layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                    parent.layer.close(parent.layer.getFrameIndex(window.name))//关闭当前页
                                    parent.layui.table.reload('user-table')
                                })
                            },
                        })



                    });
                } else {
                    layer.msg(result.msg, {icon: 2, time: 1000})
                }
            }
         })
          return false

    })
  })
</script>
<script>
</script>
</body>
</html>